﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace pswmanage
{
    public partial class Form1 : Form
    {
        DataTable dt = new DataTable();
        DataTable dtt;
        Search ss;
        public Form1()
        {
            InitializeComponent();

            ss = new Search();
        }
        private void tabPage1_Activated(object sender, EventArgs e)
        {
            this.text_key.Focus();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            dt = ss.SearchItem(this.text_key.Text.Trim(), this.fuse.Text.Trim(), this.fserver.Text.Trim(), this.fsystem.Text.Trim(), this.username.Text.Trim(), this.fnote.Text.Trim());
            this.dtt.Clear();
            foreach (DataRow r in dt.Rows)
            {
                DataRow rr = this.dtt.NewRow();
                rr["使用人"] = r["fuse"].ToString();
                rr["服务器"] = r["fserver"].ToString();
                rr["系统"] = r["fsystem"].ToString();
                rr["账户"] = r["username"].ToString();
                rr["密码"] = r["password"].ToString();
                rr["备注"] = r["fnote"].ToString();
                rr["建立日期"] = r["createdate"].ToString();
                rr["最近使用"] = r["usedate"].ToString();
                rr["使用计数"] = r["usecount"].ToString();
                this.dtt.Rows.Add(rr);
            }
            this.dtt.AcceptChanges();
            
            
            this.label2.Text = " 共 " + dt.Rows.Count.ToString() + " 条数据";
            if (dt.Rows.Count==0) {
                MessageBox.Show("没有数据，请确认密钥是否正确！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rec = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y,
                dataGridView1.RowHeadersWidth - 4, e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
                dataGridView1.RowHeadersDefaultCellStyle.Font, rec,
                dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void button5_Click(object sender, EventArgs e)  //跳转到输入密钥
        {
            tabControl1.SelectedIndex = 1;
            tabControl1.SizeMode = TabSizeMode.Fixed;
            tabControl1.ItemSize = new Size(0, 1);
            this.text_key.Text = "";
            this.button_clear_Click(sender, e);   //清空dataGridView1
            this.tabPage1.Show();
            this.tabPage2.Hide();
            this.tabPage3.Hide();
            this.tabPage4.Hide();
            this.tabPage5.Hide();
            this.groupBox1.Hide();
            this.AcceptButton = button_key;
            this.text_key.Focus();
        }

        private void button_key_Click(object sender, EventArgs e)   //输入密钥跳转到查询
        {
            if (this.text_key.Text.Trim() == "")
            {
                MessageBox.Show("请输入密钥！","提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            tabControl1.SelectedIndex = 2;
            tabControl1.SizeMode = TabSizeMode.Fixed;
            tabControl1.ItemSize = new Size(0, 1);
            this.tabPage1.Hide();
            this.tabPage2.Show();
            this.tabPage3.Hide();
            this.tabPage4.Hide();
            this.tabPage5.Hide();
            this.groupBox1.Show();
            this.AcceptButton = button1;
        }
        private void button3_Click(object sender, EventArgs e)  //跳转到新增
        {
            if (this.text_key.Text.Trim() == "")
            {
                MessageBox.Show("请输入原密钥！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            tabControl1.SelectedIndex = 3;
            tabControl1.SizeMode = TabSizeMode.Fixed;
            tabControl1.ItemSize = new Size(0, 1);
            this.tabPage1.Hide();
            this.tabPage2.Hide();
            this.tabPage3.Show();
            this.tabPage4.Hide();
            this.tabPage5.Hide();
            this.groupBox1.Show();
            this.AcceptButton = button_add;
            this.text_adduse.Focus();
        }
        
        private void button_clear_Click(object sender, EventArgs e)
        {
            this.dtt.Clear();
            this.fuse.Text = this.fserver.Text = this.fsystem.Text = this.username.Text=this.fnote.Text = "";
        }

        private void esc_Click(object sender, EventArgs e)
        {
            
            if (MessageBox.Show("确定退出？", "提示", MessageBoxButtons.OKCancel,MessageBoxIcon.Question) == DialogResult.OK)
            {
                this.Close();
            }
            return;
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            this.dtt = new DataTable();
            this.dtt.Columns.Add("使用人");
            this.dtt.Columns.Add("服务器");
            this.dtt.Columns.Add("系统");
            this.dtt.Columns.Add("账户");
            this.dtt.Columns.Add("密码");
            this.dtt.Columns.Add("备注");
            this.dtt.Columns.Add("建立日期");
            this.dtt.Columns.Add("最近使用");
            this.dtt.Columns.Add("使用计数");
            this.dataGridView1.Columns["使用人"].DataPropertyName = "使用人";
            this.dataGridView1.Columns["服务器"].DataPropertyName = "服务器";
            this.dataGridView1.Columns["系统"].DataPropertyName = "系统";
            this.dataGridView1.Columns["账户"].DataPropertyName = "账户";
            this.dataGridView1.Columns["密码"].DataPropertyName = "密码";
            this.dataGridView1.Columns["备注"].DataPropertyName = "备注";
            this.dataGridView1.Columns["建立日期"].DataPropertyName = "建立日期";
            this.dataGridView1.Columns["最近使用"].DataPropertyName = "最近使用";
            this.dataGridView1.Columns["使用计数"].DataPropertyName = "使用计数";
            this.dataGridView1.DataSource = this.dtt;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.tabControl1.SelectedIndex == 3 && e.RowIndex>=0)    //单击复制当前行到新增界面
            {
                this.text_adduse.Text =this.dataGridView1.Rows[e.RowIndex].Cells[1].EditedFormattedValue.ToString();
                this.text_addserver.Text =this.dataGridView1.Rows[e.RowIndex].Cells[2].EditedFormattedValue.ToString();
                this.text_addsystem.Text =this.dataGridView1.Rows[e.RowIndex].Cells[3].EditedFormattedValue.ToString();
                this.text_addusername.Text =this.dataGridView1.Rows[e.RowIndex].Cells[4].EditedFormattedValue.ToString();
                //this.text_addpassword.Text =this.dataGridView1.Rows[e.RowIndex].Cells[5].EditedFormattedValue.ToString();
                this.text_addnote.Text =this.dataGridView1.Rows[e.RowIndex].Cells[6].EditedFormattedValue.ToString();
            }

            if (this.tabControl1.SelectedIndex == 2 && e.RowIndex >= 0)    //单击复制当前栏位到过滤界面
            {
                switch (e.ColumnIndex)
                {
                    case 1:
                        this.fuse.Text = this.dataGridView1.Rows[e.RowIndex].Cells[1].EditedFormattedValue.ToString();
                        break;
                    case 2:
                        this.fserver.Text = this.dataGridView1.Rows[e.RowIndex].Cells[2].EditedFormattedValue.ToString();
                        break;
                    case 3:
                        this.fsystem.Text = this.dataGridView1.Rows[e.RowIndex].Cells[3].EditedFormattedValue.ToString();
                        break;
                    case 4:
                        this.username.Text = this.dataGridView1.Rows[e.RowIndex].Cells[4].EditedFormattedValue.ToString();
                        break;
                    case 6:
                        this.fnote.Text = this.dataGridView1.Rows[e.RowIndex].Cells[6].EditedFormattedValue.ToString();
                        break;
                    default:
                        break;
                }
                //
            }
        }

        private void button2_Click(object sender, EventArgs e)  //删除选中行
        {
            int m = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString().Equals("True"))
                {
                    m++;
                }
            }
            if (m == 0)
            {
                MessageBox.Show("请勾选需要删除的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (MessageBox.Show("确定要删除" + m + "行账号？","提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                m = 0;
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString().Equals("True"))
                    {
                        int j = ss.DelPSW(this.text_key.Text.Trim(), dataGridView1.Rows[i].Cells[1].EditedFormattedValue.ToString(), dataGridView1.Rows[i].Cells[2].EditedFormattedValue.ToString(), dataGridView1.Rows[i].Cells[3].EditedFormattedValue.ToString(), dataGridView1.Rows[i].Cells[4].EditedFormattedValue.ToString()
                             );
                        m = (j == 1) ? m + 1 : m;
                    }
                }
                MessageBox.Show("已删除" + m + "行。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.button1_Click(sender, e);
            }
        }

        private void button4_Click(object sender, EventArgs e)  //跳转到更改密钥
        {
            int m = 0;
            int i = 0;
            for (; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString().Equals("True"))
                {
                    m++;
                }
            }
            if (m == 0)
            {
                MessageBox.Show("请勾选需要更改的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (m < i && MessageBox.Show("总计" + i + "行,确定只更新" + m + "行？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            this.text_newkey.Text = "";
            tabControl1.SelectedIndex = 4;
            tabControl1.SizeMode = TabSizeMode.Fixed;
            tabControl1.ItemSize = new Size(0, 1);
            this.tabPage1.Hide();
            this.tabPage2.Hide();
            this.tabPage3.Hide();
            this.tabPage4.Show();
            this.tabPage5.Hide();
            this.groupBox1.Show();
            this.AcceptButton = button_savenewkey;
            this.text_newkey.Focus();
        }

        private void button_share_Click(object sender, EventArgs e)  //分享
        {
            int m = 0;
            int i = 0;
            for (; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString().Equals("True"))
                {
                    m++;
                }
            }
            if (m == 0)
            {
                MessageBox.Show("请勾选需要分享的行", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            else if (m < i && MessageBox.Show("确定分享" + m + "行？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                return;
            }

            this.text_newkey1.Text = "";
            this.text_newfuse1.Text = "";
            tabControl1.SelectedIndex = 5;
            tabControl1.SizeMode = TabSizeMode.Fixed;
            tabControl1.ItemSize = new Size(0, 1);
            this.tabPage1.Hide();
            this.tabPage2.Hide();
            this.tabPage3.Hide();
            this.tabPage4.Hide();
            this.tabPage5.Show();
            this.groupBox1.Show();
            this.AcceptButton = button_sharesave;
            this.text_newfuse1.Focus();
        }
        private void button_sharesave_Click(object sender, EventArgs e)  //分享到新用户
        {
            if (this.text_newkey1.Text.Trim() == "" || this.text_newfuse1.Text.Trim() == "")
            {
                MessageBox.Show("请输入新用户和新密钥！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int m = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString().Equals("True"))
                {
                    int j = ss.CopyPSW(this.text_key.Text.Trim(), this.text_newkey1.Text.Trim(), this.text_newfuse1.Text.Trim(), dataGridView1.Rows[i].Cells[1].EditedFormattedValue.ToString(),  dataGridView1.Rows[i].Cells[2].EditedFormattedValue.ToString(), dataGridView1.Rows[i].Cells[3].EditedFormattedValue.ToString(), dataGridView1.Rows[i].Cells[4].EditedFormattedValue.ToString()
                         );
                    m = (j == 1) ? m + 1 : m;
                }
            }
            MessageBox.Show(m + "行账号已分享给"+ this.text_newfuse1.Text.Trim() +"。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.text_key.Text = this.text_newkey1.Text.Trim();
            this.button_key_Click(sender, e);
            this.button1_Click(sender, e);
        }
        private void button_savenewkey_Click(object sender, EventArgs e)  //更新密钥
        {
            if (this.text_newkey.Text.Trim() == "")
            {
                MessageBox.Show("请输入原密钥！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int m = 0;
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (dataGridView1.Rows[i].Cells[0].EditedFormattedValue.ToString().Equals("True"))
                {
                    int j = ss.ChgPSW(this.text_key.Text.Trim(), this.text_newkey.Text.Trim(), dataGridView1.Rows[i].Cells[1].EditedFormattedValue.ToString(), dataGridView1.Rows[i].Cells[2].EditedFormattedValue.ToString(), dataGridView1.Rows[i].Cells[3].EditedFormattedValue.ToString(), dataGridView1.Rows[i].Cells[4].EditedFormattedValue.ToString()
                         );
                    m = (j == 1) ? m + 1 : m;
                }
            }
            MessageBox.Show(m + "行密钥已更新，使用新密钥查询。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //this.text_key.Text = this.text_newkey.Text.Trim();
            this.button_key_Click(sender, e);
            this.button1_Click(sender, e);
        }
        private void button_add_Click(object sender, EventArgs e)   //新增保存
        {
            if (this.text_adduse.Text.Trim() == "" || this.text_addserver.Text.Trim() == "" || this.text_addsystem.Text.Trim() == "" || this.text_addusername.Text.Trim() == "" || this.text_addpassword.Text.Trim() == "")
            {
                MessageBox.Show("除备注外不可为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int j = ss.AddPSW(this.text_key.Text.Trim(), this.text_adduse.Text.Trim(),this.text_addserver.Text.Trim(),this.text_addsystem.Text.Trim(), this.text_addusername.Text.Trim(), this.text_addpassword.Text.Trim(), this.text_addnote.Text.Trim());

            if (j == 1) { 
                MessageBox.Show("新增成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.button_key_Click(sender, e);
            }else{
                MessageBox.Show("保存失败,请确认账号是否已存在！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            this.button1_Click(sender, e);
        }

    }
}
